package com.zoroki.student_score_system.controller;

import com.zoroki.student_score_system.domain.User;
import com.zoroki.student_score_system.service.UserService;
import com.zoroki.student_score_system.utils.JWTUtils;
import com.zoroki.student_score_system.utils.Msg;
import com.zoroki.student_score_system.vo.Result;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody User user) {
//        System.out.println(user.getUsername() + user.getPassword());
        User loginUser = userService.userLogin(user.getUsername(), user.getPassword());
        System.out.println(loginUser.getUsername()+"登录了...");
        if (loginUser == null) {
            return new Result(-1,null,Msg.SUCCESS);
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("username", user.getUsername());
        map.put("password", user.getPassword());
        String token = JWTUtils.getToken(map);
        return new Result(0,token, Msg.SUCCESS);
    }

}
